package com.ler.yrmall.coupon.rpc;

import cn.hutool.core.bean.BeanUtil;
import com.ler.yrmall.basic.Result;
import com.ler.yrmall.coupon.api.CouponApi;
import com.ler.yrmall.coupon.dto.CouponDTO;
import com.ler.yrmall.coupon.dto.SeckillSkuDTO;
import com.ler.yrmall.coupon.dto.SpuBoundDTO;
import com.ler.yrmall.coupon.entity.CouponEntity;
import com.ler.yrmall.coupon.entity.SpuBoundsEntity;
import com.ler.yrmall.coupon.service.CouponService;
import com.ler.yrmall.coupon.service.SeckillService;
import com.ler.yrmall.coupon.service.SpuBoundsService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @author Leron
 */
@Service
@DubboService
public class CouponApiImpl implements CouponApi {

    @Resource
    private SpuBoundsService spuBoundsService;

    @Resource
    private CouponService couponService;

    @Resource
    private SeckillService seckillSessionService;

    @Override
    public Result<?> saveSpuBounds(SpuBoundDTO data) {
        SpuBoundsEntity target = new SpuBoundsEntity();
        BeanUtils.copyProperties(data,target);
        spuBoundsService.save(target);
        return Result.ok();
    }

    @Override
    public Result<SeckillSkuDTO> getSkuSeckill(Long skuId) {
        return Result.ok(seckillSessionService.getSkuSeckill(skuId));
    }

    @Override
    public Result<CouponDTO> computeBestCoupon(List<Long> spuIdList, BigDecimal amount) {
        return Result.ok(couponService.computeBestCoupon(spuIdList,amount));
    }

    @Override
    public Result<CouponDTO> getCouponById(Long couponId) {
        CouponEntity coupon = couponService.getById(couponId);
        return Result.ok(BeanUtil.copyProperties(coupon,CouponDTO.class));
    }

    @Override
    public Result<CouponDTO> useCoupon(Long couponId, BigDecimal amount) {
        return Result.ok(couponService.useCoupon(couponId,amount));
    }

}
